<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/myStrategyComment.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script>
        $(function () {
            //拿到大攻略的id
            var strategyId = getParams().strategyId;
            var currentImgBtn;
            var grade;
            var labels = new Array();
            //图片路径数组
            var images = new Array();
            //图片路径数组索引
            var urlIndex=0;
            //评分星级点击事件
            $(".star i").click(function () {
                $(".star i").prop("class", "fa-star-o fa fa-lg");
                //获取点击的星级
                var num = $(this).data("num");
                console.log(num);
                //实现星级的更新
                $.each($(".star i"), function (index, item) {
                    if (index + 1 <= num) {
                        $(item).prop("class", "fa-star fa fa-lg");
                    }
                })
                //赋值到表单
                grade = num;
            })
            //标签复选框的信息封装
            $(".btn-group-toggle").on("click", ".btn", function () {
                var currentInput = $(this).find("input");
                //如果点击设置复选框为选中
                //console.log(currentInput);
                currentInput.prop('checked', !(currentInput.prop('checked')));
                var inputs  = $(".btn-group-toggle input");
                //清空数组内容
                console.log(inputs);
                labels.length = 0;
                //遍历所有的复选框标签
                $.each(inputs, function (index, item) {
                    //console.log(index);
                    //console.log($(item).prop('checked'));
                    //如果为选中的标签,保存在数组中
                    if ($(item).prop('checked') == true) {
                        labels[index] = item.value;
                    }
                    index++;
                })
                console.log(labels);
                //赋值选择标签到表单
            })

            //获取所有的标签模板
            $.get('/strategys/comment/modelLabels', function (data) {
                console.log(data);
                $(".tag").renderValues({list: data}, {
                    handler: function (ele, value) {
                        //使用span标签用于显示标签信息
                        var input = $('<span></span>').html(value);
                        $(ele).append(input);
                    }
                });
            })

            //点击上传模块获取图片存储路径
            $(".col-3 span").click(function () {
                //文件上传控件点击动作
                $("#imgForm input[name='pic']").click();
                //提交表单获取图片存储路径
                currentImgBtn = $(this);
            })
            //表单值改变,说明有选择图片
            $("#imgForm input[name='pic']").change(function () {
                if($(this).val()){
                    $("#imgForm").ajaxSubmit(function (data) {
                        if(data.status==1){
                            //展示选中的图片
                            currentImgBtn.closest('div').html($("<img src='"+data.url+"'/>"));
                            images[urlIndex]=data.url;
                            urlIndex++;
                            console.log(images);
                        }else{
                            $(document).dialog({
                                titleShow: false,
                                content: data.msg,
                                autoClose: 1500
                            });
                        }
                    })
                }
            })
            //提交按钮实现
            $("#submitBtn").click(function () {
                //使用自定ajax请求提交数据
                $(document).dialog({
                    type : 'confirm',
                    style: 'android',
                    titleText: '温馨提示',
                    content: '确定提交?',
                    buttons: [
                        {
                            name: '确定',
                            callback: function() {
                               var myTags = $(".comment input[name='myTags']").val();
                               var content = $(".comment textarea[name='content']").val();
                                console.log(content);
                                //imges表单赋值
                               $.ajax({
                                   url:"/strategys/"+strategyId+"/comment",
                                   dataType:"json",
                                   type:"post",
                                   data:{"labels":labels,"images":images,"grade":grade,
                                       "myTags":myTags,"content":content,
                                   },
                                   traditional: true,
                                   success:function (data) {
                                       if(data.success){
                                           location.href="/strategyCatalogs.html?strategyId="+strategyId;
                                       }else{
                                           $(document).dialog({
                                               titleShow: false,
                                               content: data.msg,
                                               autoClose: 1500
                                           });
                                       }
                                   }
                                   }
                               )
                            }
                        },
                        {
                            name: '取消',
                            callback: function() {

                            }
                        }
                    ]
                });

            })

        })
    </script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
    <script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
    <script src="/js/common.js"></script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:window.history.go(-1)">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>

        </div>
        <div class="col">
            <span>点评</span>
        </div>
        <div class="col"></div>
    </div>
</div>

    <div class="comment">
        <div class="container star">
            <p>你对该旅游地的评价</p>
            <i class="fa fa-star-o fa-lg" data-num="1"></i>
            <i class="fa fa-star-o fa-lg" data-num="2"></i>
            <i class="fa fa-star-o fa-lg" data-num="3"></i>
            <i class="fa fa-star-o fa-lg" data-num="4"></i>
            <i class="fa fa-star-o fa-lg" data-num="5"></i>
        </div>
        <hr/>
        <div class="container tag">
            <p>请为该旅游地选择合适的标签（大家都在评）</p>
            <div class="btn-group-toggle" data-toggle="buttons" render-loop="list">

                <label class="btn btn-outline-danger"  render-key="list.content"
                       render-fun="handler">

                    <input type="checkbox" autocomplete="off" render-value="list.id">
                </label>
            </div>

            <p>添加你自己的标签</p>
            <input name="myTags" class="form-control" placeholder="个人标签"/>

            <p>更多评价内容</p>
            <textarea name="content" class="form-control" rows="5" placeholder="请填写你对该旅游地的评价"></textarea>
        </div>
    </div>
    <div>
        <div class="container imgs ">
            <p>上传图片</p>

            <div class="row">
                <form id="imgForm" method="post" action="/images" enctype="multipart/form-data">
                    <input type="file" style="display: none" name="pic"/>
                </form>
                <div class="col-3 rect">
                    <span>上传</span>
                </div>
                <div class="col-3 rect">
                    <span>上传</span>
                </div>
                <div class="col-3 rect">
                    <span>上传</span>
                </div>
                <div class="col-3 rect">
                    <span>上传</span>
                </div>
                <div class="col-3 rect">
                    <span>上传</span>
                </div>
            </div>
        </div>

    </div>


    <div class="operation">
        <button class="btn" onclick="window.history.go(-1)">放弃评价</button>
        <button class="btn" id="submitBtn">提交评价</button>
    </div>


</body>


</html>